<?php
Zend_Loader::loadClass("BaseControllerAction");
Zend_Loader::loadClass("Application_Ajax_Server");

class AjaxController extends BaseControllerAction
{
	private $_params = array();
	const BROWSE_MIN_LENGTH = 3;
	const BROWSE_FIELD_ID = "id";
	const BROWSE_FIELD_NAME = "name";
	
	public function init()
	{
		parent::init();
		$this->_helper->contextSwitch()->setAutoJsonSerialization(false);
		$this->_helper->viewRenderer->setNoRender(true);
		$this->_helper->layout->disableLayout();
		//$this->_helper->contextSwitch()->addActionContext('ajax', 'json')->initContext();
	}

	public function ajaxAction() {
		$server = new Application_Ajax_Server();
		$server->setRequest($this->getRequest()->getParam('function'));
		$server->setParams($this->getRequest()->getParams());
		$ret = $server->call();
		if ($ret) {
			$results = $server->getResponse();
		}
		else {
			header("HTTP/1.0 500 Ajax Error", true, 500);
			$results = false;
		}
		if ($this->getRequest()->getParam('debug')) {
			echo Zend_Json::prettyPrint(Zend_Json::encode($results), "\t");
		} else {
			echo Zend_Json::encode($results);
		}
	}
}
